/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.HistoryPenghuniDAO;
import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.dao.impl.HistoryPenghuniDAOImpl;
import com.ivanbiz.dao.impl.KamarDAOImpl;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.service.ServiceHelper;
import java.text.NumberFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class SewaKamarPanel extends javax.swing.JPanel {

    List<Kamar> listKamar;
    NumberFormat numberFormat;
    String fasilitas;
    JDialog jDialogFasilitas;
    Kamar kamarFasilitas;

    public Kamar getKamarFasilitas() {
        return kamarFasilitas;
    }

    public void setKamarFasilitas(Kamar kamarFasilitas) {
        this.kamarFasilitas = kamarFasilitas;
    }

    public SewaKamarPanel() {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKamar = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonTambah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Sewa Kamar");

        tableKamar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableKamar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableKamarMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableKamar);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah / Ubah Penghuni Kamar Terseleksi");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonTambah);

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Tutup Penghuni Kamar Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });
        jPanel1.add(buttonHapus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        if (tableKamar.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan ditambah / diubah", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            Kamar kamar = listKamar.get(tableKamar.getSelectedRow());
            JDialog jDialog = new JDialog();
            jDialog.setModal(true);
            jDialog.setSize(435, 335);
            jDialog.add(new SewaKamarUpdatePanel(jDialog, kamar));
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
            refresh();
        }
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableKamar.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            int response = JOptionPane.showConfirmDialog(this, "Apakah Penghuni ingin ditutup ?", "Konfirmasi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                try {
                    Kamar kamar = listKamar.get(tableKamar.getSelectedRow());
//                KamarDAO kamarDAO = new KamarDAOImpl();
//                kamar.setPenghuni(null);
//                kamar.setTarif(0);
//                kamarDAO.update(kamar);
                    HistoryPenghuniDAO historyPenghuniDAO = new HistoryPenghuniDAOImpl();
                    historyPenghuniDAO.tutupPenghuni(kamar.getPenghuni());
                    refresh();
                } catch (Exception ex) {
                    Logger.getLogger(SewaKamarPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed

    private void tableKamarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableKamarMouseClicked
        if (fasilitas != null) {
            if (evt.getClickCount() == 2) {
                kamarFasilitas = listKamar.get(tableKamar.getSelectedRow());
                jDialogFasilitas.dispose();
            }
        }
    }//GEN-LAST:event_tableKamarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableKamar;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            KamarDAO kamarDAO = new KamarDAOImpl();
            listKamar = kamarDAO.getAll(Kamar.class);
            updateTableKamar();
        } catch (Exception ex) {
            Logger.getLogger(SewaKamarPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableKamar() {
        String[] judul = {"No", "Kos", "No Kamar", "Tarif", "Nama Penghuni", "Telp/HP Penghuni", "Alamat Penghuni"};
        Object[][] isi = new Object[listKamar.size()][7];
        int x = 0;
        int no = 0;
        String kode = "";
        for (Kamar kamar : listKamar) {
            if (!kode.equals(kamar.getKos().getKodeKos())) {
                no += 1;
                isi[x][0] = no;
                isi[x][1] = kamar.getKos().getNama();
            } else {
                isi[x][0] = "";
                isi[x][1] = "";
            }
            isi[x][2] = kamar.getKodeKamar();
            isi[x][3] = kamar.getTarif() == 0 ? "" : numberFormat.format(kamar.getTarif());
            isi[x][4] = kamar.getPenghuni() == null ? "" : kamar.getPenghuni().getNama();
            isi[x][5] = kamar.getPenghuni() == null ? "" : kamar.getPenghuni().getTelp();
            isi[x][6] = kamar.getPenghuni() == null ? "" : kamar.getPenghuni().getAlamat();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableKamar);
    }
}
